#!/bin/sh
###############################################################################
##  Author    : xuezhoyi
##  Name      : edw_parallel_proc_launcher.sh
##  Functions : 多线程执行存储过程
##  Purpose   : 
##  Revisions or Comments
##  VER        DATE        AUTHOR           DESCRIPTION
##---------  ----------  ---------------  ------------------------------------ 
##  1.0      2017-05-22  xuezhouyi        1. CREATED THIS SHELL.
##  2.0      2018-11-11  薛周毅           1.根据参数V_PARALLEL_PROC_NO的线程数进行并发
##                                        2.如果表【EDW_JOB_STATUS】中没有作业则退出
##                                        3.并发后会检查每个进程的状态
###############################################################################

. ${0%`basename ${0}`}edw_parm.sh
. ${0%`basename ${0}`}edw_func.sh

function USAGE(){
    echo "How to use this shell script!"
    echo "${V_SHELL_HOME}/edw_parallel_proc_launcher.sh HIVE"
    echo "${V_SHELL_HOME}/edw_parallel_proc_launcher.sh MYSQL"
}

# Check variable
if [[ -z ${1} ]];then
    USAGE
    exit 1
else
    V_JOB_TYPE=${1}
fi

V_DATA_DATE=$(grep "#V_DATA_DATE#" ${V_PARM_FILE} | awk -F'=' '{print $2}')
V_FREQUENCY=$(grep "#V_FREQUENCY#" ${V_PARM_FILE} | awk -F'=' '{print $2}')
# Define basic parm
V_SCRIPT_NAME=$(basename ${0} | cut -d'.' -f1)
V_OUT_LOGS="${V_SHELL_LOGS}/${V_DATA_DATE}/${V_SCRIPT_NAME}_${V_FREQUENCY}.log"

# Define the output flow
exec 4>&1               # screen output
exec 3>>${V_OUT_LOGS}   # script output
exec 2>&3               # error output
exec 1>&3               # standard output

BEGIN
# For rerun and initinal status is null
V_QUERY="UPDATE ETL.EDW_JOB_STATUS SET STATUS=NULL WHERE DATA_DATE='${V_DATA_DATE}' AND STATUS <>'DONE'"
${V_RUN_DB} -q "${V_QUERY}" || exit 2


# Check job count
V_QUERY="SELECT COALESCE(COUNT(1),0) FROM ETL.EDW_JOB_STATUS WHERE DATA_DATE='${V_DATA_DATE}' AND STATUS IS NULL"
V_JOB_CNT=$(${V_RUN_DB} -q "${V_QUERY}") || exit 3
if [[ ${V_JOB_CNT} -eq 0 ]];then
    LOGGER "WARNING" "No job to run."
    END
    exit 0
else
    LOGGER "INFO" "Has ${V_JOB_CNT} jobs to run."
    echo "JOB_CNT=${V_JOB_CNT}" >> ${PROC_SPEED_FILE}
fi
# Parallel run procedure
LOGGER "INFO" "[------------------------Parallel Run Procedure------------------------]"
for ((i=0; i<${V_PARALLEL_PROC_NO}; i++));do
    sh ${V_RUN_PROCS} ${V_JOB_TYPE} ${i}&
    V_PROC_PID[i]=$(echo ${!})
    LOGGER "INFO" "Start to call[${V_PROC_PID[i]}]:${V_RUN_PROCS} ${V_JOB_TYPE} ${i}"
done
    
# Check the procedure PID
LOGGER "INFO" "Starting to check procedure..."
for ((i=0; i<${#V_PROC_PID[@]}; i++));do
    # Get the return code
    wait ${V_PROC_PID[i]}
    RE_CODE=$?
    if [[ ${RE_CODE} -eq 0 ]];then
        LOGGER "INFO" "线程${i} PID=${V_PROC_PID[i]} 运行完成."
        
    else
        LOGGER "ERROR ${RE_CODE}" "[线程${i} PID=${V_PROC_PID[i]}]:运行失败. 日志:[edw_proc_launcher_${V_JOB_TYPE}_${V_FREQUENCY}_${i}.log]"
        x=fail
    fi
done
rm ${PROC_SPEED_FILE}
# Check all
if [[ ${x} == "fail" ]];then
    exit 4
fi

END
exit 0
